#!/bin/bash 
set -e


TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage4}
SRC=${SRC:-/sources}

name=docbook-xsl
version=1.79.2

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-nons-$version.tar.bz2 -C $TMP

{ time \
   {

    cd $TMP/$name-nons-$version
    
	patch -Np1 -i  $SRC/$name-nons-$version-stack_fix-1.patch

	tar -xf  $SRC/$name-doc-$version.tar.bz2 --strip-components=1

	install -v -m755 -d /usr/share/xml/docbook/xsl-stylesheets-nons-$version

	cp -v -R VERSION assembly common eclipse epub epub3 extensions fo        \
	         highlighting html htmlhelp images javahelp lib manpages params  \
	         profiling roundtrip slides template tests tools webhelp website \
	         xhtml xhtml-1_1 xhtml5                                          \
	    /usr/share/xml/docbook/xsl-stylesheets-nons-$version

	ln -s VERSION /usr/share/xml/docbook/xsl-stylesheets-nons-$version/VERSION.xsl

	install -v -m644 -D README \
	                    /usr/share/doc/$name-nons-$version/README.txt
	install -v -m644    RELEASE-NOTES* NEWS* \
	                    /usr/share/doc/$name-nons-$version

	cp -v -R doc/* /usr/share/doc/$name-nons-$version

	if [ ! -d /etc/xml ]; then install -v -m755 -d /etc/xml; fi
	if [ ! -f /etc/xml/catalog ]; then
	    xmlcatalog --noout --create /etc/xml/catalog
	fi &&

	xmlcatalog --noout --add "rewriteSystem" \
	           "https://cdn.docbook.org/release/xsl-nons/$version" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

	xmlcatalog --noout --add "rewriteURI" \
	           "https://cdn.docbook.org/release/xsl-nons/$version" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

	xmlcatalog --noout --add "rewriteSystem" \
	           "https://cdn.docbook.org/release/xsl-nons/current" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

	xmlcatalog --noout --add "rewriteURI" \
	           "https://cdn.docbook.org/release/xsl-nons/current" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

	xmlcatalog --noout --add "rewriteSystem" \
	           "http://docbook.sourceforge.net/release/xsl/current" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

	xmlcatalog --noout --add "rewriteURI" \
	           "http://docbook.sourceforge.net/release/xsl/current" \
	           "/usr/share/xml/docbook/xsl-stylesheets-nons-$version" \
	    /etc/xml/catalog

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
